package cn.uai.activiti8.approval.bus;

import cn.uai.activiti8.dto.Result;
import cn.uai.activiti8.service.UserService;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.HashMap;
import java.util.Map;

import static cn.uai.activiti8.enums.RespCodeEnum.TASK_NOT_FOUND;

/**
 * @desc 业务总线抽象实现类
 * @author uai
 * @since 2025/8/7 10:46
 * @version 1.0
 */
public abstract class AbstractBusinessBus implements BusinessBus {

    @Resource
    private UserService userService;

    @Resource
    private TaskService taskService;

    @Override
    public ProcessInstance start(String body) {
        return null;
    }

    @Override
    public ProcessInstance start(String processDefinitionId, Map<String, Object> variables) {
        return null;
    }

    @Override
    public ProcessInstance startByKey(String processDefinitionKey, Map<String, Object> variables) {
        return null;
    }

    @Override
    public ProcessInstance startByKey(String processDefinitionKey, String businessKey, Map<String, Object> variables) {
        return null;
    }

    @Override
    public String complete(String body) {
        Map<String, Object> variable = new HashMap<>();
        JSONObject bodyJson = JSONObject.parseObject(body);
        String taskId = bodyJson.getString("taskId");
        Integer actionType = bodyJson.getInteger("actionType");
        Long uid = bodyJson.getLong("uid");
        Long rid = userService.findRidByUid(uid);
        variable.put("actionType", actionType);
        boolean isPass = false;
        switch (actionType) {
            case 1 -> isPass = true;
            case 2, 3 -> isPass = false;
        }
        variable.put("isPass", isPass);
        dynamicAssignCompleteVariables(rid, variable);
        try {
            taskService.complete(taskId, variable);
        } catch (ActivitiObjectNotFoundException e) {
            return Result.exception(TASK_NOT_FOUND.getCode(), "任务 taskId [" + taskId + "] 已完成，或输入的taskId不存在");
        }
        return Result.success();
    }

    @Override
    public String completeDelegation(String body) {
        return null;
    }

    @Override
    public String claim(String body) {
        return null;
    }

    @Override
    public String assign(String body) {
        return null;
    }

    @Override
    public String delegate(String body) {
        return null;
    }

    @Override
    public String reject(String body) {
        return null;
    }

    @Override
    public String reject2Apply(String body) {
        return null;
    }

    @Override
    public String reject2Any(String body) {
        return null;
    }

    @Override
    public String taskQuery(Long uid) {
        return null;
    }

    @Override
    public String taskQuery(String userName) {
        return null;
    }

    @Override
    public String candidateUserTaskQuery(String userName) {
        return null;
    }

    @Override
    public String candidateGroupTaskQuery(String groupName) {
        return null;
    }

    @Override
    public String suspend(String body) {
        return null;
    }

    @Override
    public String activate(String body) {
        return null;
    }

    /**
     * 动态赋值启动流程变量
     * @param original 原始变量
     * @param variables 变量集合
     */
    protected abstract void dynamicAssignStartVariables(Object original, Map<String, Object> variables);

    /**
     * 动态赋值完成审批变量
     * @param original 原始变量
     * @param variables 变量集合
     */
    protected abstract void dynamicAssignCompleteVariables(Object original, Map<String, Object> variables);
}
